package com.weixin.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixin.common.mapper.SysUserMapper;
import com.weixin.common.mapper.SysUserRoleMapper;
import com.weixin.common.model.dto.SaveUserRoleDto;
import com.weixin.common.model.dto.UserAddDto;
import com.weixin.common.model.entity.SysRole;
import com.weixin.common.model.entity.SysUser;
import com.weixin.common.model.entity.SysUserRole;
import com.weixin.common.model.so.SimpleSo;
import com.weixin.common.model.vo.SysUserVo;
import com.weixin.common.security.JwtUser;
import com.weixin.common.service.SysUserService;
import com.weixin.common.utils.AuthUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    AuthUtils authUtils;

    @Override
    public SysUser getByUsername(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserName, username);
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public SysUser getById(Long userId) {
        return sysUserMapper.selectById(userId);
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        return true;
    }

    @Override
    public boolean addUser(UserAddDto dto) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(dto, sysUser);
        sysUser.setTenantId(Long.parseLong(dto.getTenantId()));
        sysUser.setPassword(authUtils.passwordEncoder().encode(dto.getPassword()));
        return sysUserMapper.insert(sysUser) > 0;
    }

    @Override
    public List<SysRole> listRoleByUserId(Long userId) {
        return sysUserMapper.listRoleByUserId(userId);
    }

    /**
     * 根据条件查询用户列表
     *
     * @param page 分页参数
     * @param so   查询条件
     * @return 用户列表
     */
    @Override
    public IPage<SysUserVo> listUser(Page page, SimpleSo so) {
        JwtUser jwtUser = authUtils.getUser();
        boolean isSuperAdmin = authUtils.isSuperAdmin();
        IPage<SysUserVo> sysUsers = sysUserMapper.pageUser(page, so.getKeyword(),isSuperAdmin?null:jwtUser.getTenantId());
        return sysUsers;
    }


    @Override
    public JwtUser getUserInfo() {
        return authUtils.getUser();
    }

    @Override
    public boolean deleteUser(Long userId) {
        sysUserMapper.deleteById(userId);
        return false;
    }

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Transactional
    @Override
    public boolean saveRole(SaveUserRoleDto dto) {
        List<Long> roleIds = dto.getRoleIds();
        sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, dto.getUserId()));
        roleIds.forEach(roleId -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(dto.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleMapper.insert(sysUserRole);
        });
        return true;
    }


}
